package com.weichat.controller;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.model.Event_user;
import com.model.Propertr_user;
import com.model.User;
import com.service.Event_userService;
import com.service.Propertr_userService;
import com.service.UserService;
import com.weichat.jwh.tool.MediaUtil_jwh;
import com.weichat.jwh.tool.Signatrue_jwh;
import com.weichat.jwh.tool.TokenSaveRead_jwh;
import com.weichat.jwh.tool.WeiChatConfig_jwh;
import com.weichat.tool.AccessTokentool;
import com.weichat.tool.WeiChatConfig;
@RestController
public class WebController_jwh {
@Autowired
Event_userService event_userService;
@Autowired
Propertr_userService propertr_userService;
@Autowired
MediaUtil_jwh messageUtil;
@Autowired
AccessTokentool accessTokentool;

	
	@RequestMapping("/iniConfig/{html}")
	public Map<String,String> getJSDKconfig(HttpServletRequest req,@PathVariable String html,@RequestParam("html2") String htmlnow) throws Exception{
		Map<String,String>  configParam=new HashMap();
		configParam.put("button", "Y");
		
		System.out.println("html-------"+html);
		User u=(User) req.getSession().getAttribute("user");
		Event_user eu=event_userService.selectEidByUidOrder(u.getId());
		System.out.println("euid------"+eu.getEventId());
		if(html.equals("setPhone-jwh")){
			if(eu.getEventId()>=4){
				configParam.put("button", "N");
			}
		}else if(html.equals("breakUp-jwh")){
			if(eu.getEventId()>=6){
				configParam.put("button", "N");
			}
		}else{
			if(eu.getEventId()>=8){
				configParam.put("button", "N");
			}
		}
		
		
		
		SortedMap<String, String> finalpackage = new TreeMap<String, String>();
	 
		
		
		String timestamp = Signatrue_jwh.getTimeStamp();
		String nonce=Signatrue_jwh.getNonceStr();
		String token=(String)accessTokentool.getNewAccessToken().get("rs");
		System.out.println("token-------"+token);
		//finalpackage.put("appId", WeiChatConfig.appid);
		finalpackage.put("timestamp", timestamp);
		finalpackage.put("noncestr",nonce );
		finalpackage.put("jsapi_ticket", Signatrue_jwh.getjsApiTicket(Signatrue_jwh.ticketUrl,token));
		finalpackage.put("url", htmlnow);
		//finalpackage.put("package", packages);
		//finalpackage.put("signType", WeiChatConfig.signType);
		String signature =Signatrue_jwh.createSHA1Sign(finalpackage);
		
		
		configParam.put("appid", WeiChatConfig_jwh.appid);
		configParam.put("timestamp", timestamp);
		configParam.put("nonceStr", nonce);
		configParam.put("signature", signature);
		
		
		
		return configParam;
		
	}
	@RequestMapping("/iniConfigSetPhone")
	public Map<String,String> getJSDKconfigSP() throws Exception{
		Map<String,String>  configParam=new HashMap();
		/**
		 从数据库里拿 状态id ---
		 			乘车id 
		 				判断时间    	 有乘车（8：00-8：30）	 没乘车（8：00-9：00）	 时间内Y	时间外N 
		 			存放手机id
		 				N
		 */
		Calendar now = Calendar.getInstance();
		//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss EEEE");  
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE");  
        String dateNowStr = sdf.format(now.getTimeInMillis()); 
        SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm");  
        String dateNowStr1 = sdf1.format(now.getTimeInMillis()); 
        int hour=Integer.parseInt(dateNowStr1.substring(0,dateNowStr1.indexOf(":")));
        int min=Integer.parseInt(dateNowStr1.substring(dateNowStr1.indexOf(":")+1, dateNowStr1.length()));
        
        
	Event_user eu=	event_userService.selectEidByUidOrder(123);
	int eventId= eu.getEventId();
	if(dateNowStr.equals("星期一")||dateNowStr.equals("星期三")||dateNowStr.equals("星期五")){
		if(eventId==1){
			// 有乘车（8：00-8：30）
			if(hour==8&&min<30){
				
			}
		}else if(eventId==2){
			//没乘车（8：00-9：00）
		}else{
			
		}
	}
	
		
		return configParam;
		
	}
	@RequestMapping("/SetPhone")
	public Map<String,String> setPhone(HttpServletRequest req) throws Exception{
		Map<String,String>  m=new HashMap();
		m.put("rs", "Y");
	User u=(User)	req.getSession().getAttribute("user");
	int uid=u.getId();
		Event_user eu=new Event_user();
		eu.setEventId(4);
		eu.setUserId(u.getId());
		eu.setType("Y");
		event_userService.insertSelective(eu);
		int eu_id=event_userService.selectEidByUidOrder(uid).getEuId();

		
		Propertr_user pu=new Propertr_user();
		pu.setCount(100);
		pu.setuId(uid);
	 pu.setEuId(eu_id);
	 pu.setEventName("早上放手机");
		pu.setpId(3);
		propertr_userService.insertSelective(pu);
		pu=new Propertr_user();
		pu.setCount(100);
		pu.setuId(uid);
		pu.setEuId(eu_id);
		 pu.setEventName("早上放手机");
		pu.setpId(2);
		propertr_userService.insertSelective(pu);
		
		return m;
		
	}
	@RequestMapping("/BreakUp")
	public Map<String,String> breakUp(HttpServletRequest req) throws Exception{
		User u=(User)	req.getSession().getAttribute("user");
		int uid=u.getId();
		
		
		Map<String,String>  m=new HashMap<String, String>();
		m.put("rs", "Y");
		
		Event_user eu=new Event_user();
		eu.setEventId(6);
		eu.setUserId(uid);
		eu.setType("Y");
		
		event_userService.insertSelective(eu);
		int eu_id=event_userService.selectEidByUidOrder(uid).getEuId();
		Propertr_user pu=new Propertr_user();
		pu.setCount(100);
		pu.setuId(uid);
		pu.setEuId(eu_id);
		 pu.setEventName("午睡起床");

		pu.setpId(3);
		propertr_userService.insertSelective(pu);
		pu=new Propertr_user();
		pu.setCount(100);
		pu.setuId(uid);
		pu.setEuId(eu_id);
		pu.setpId(2);
		 pu.setEventName("午睡起床");

		propertr_userService.insertSelective(pu);
		
		pu=new Propertr_user();
		pu.setCount(5);
		pu.setuId(uid);
		pu.setEuId(eu_id);
		 pu.setEventName("午睡起床");

		pu.setpId(1);
		propertr_userService.insertSelective(pu);
		
		return m;
		
	}
	
	@RequestMapping("/uploadImg")
	public Map<String,String> uploadImg(HttpServletRequest req,@RequestParam String serverId) throws Exception{
		
		User u=(User)req.getSession().getAttribute("user");
		int uid=u.getId();
		
		
		Map<String,String>  m=new HashMap<String, String>();
		m.put("rs", "Y");
		
		
		Event_user eu=new Event_user();
		eu.setEventId(10);
		eu.setUserId(uid);
		eu.setType("Y");
		
		event_userService.insertSelective(eu);
		
		int eu_id=event_userService.selectEidByUidOrder(uid).getEuId();
		
		Propertr_user pu=new Propertr_user();
		pu.setCount(100);
		pu.setuId(uid);
		pu.setEuId(eu_id);
		pu.setpId(2);
		 pu.setEventName("天气原因");

		propertr_userService.insertSelective(pu);
		pu=new Propertr_user();
		pu.setCount(100);
		pu.setuId(uid);
		pu.setEuId(eu_id);
		pu.setpId(3);
		 pu.setEventName("天气原因");

		propertr_userService.insertSelective(pu);
		
		pu=new Propertr_user();
		pu.setCount(10);
		pu.setuId(uid);
		pu.setEuId(eu_id);
		pu.setpId(1);
		 pu.setEventName("天气原因");

		propertr_userService.insertSelective(pu);
		
		
		System.out.println("serverId-------"+serverId);
		messageUtil.saveImageToLocal(serverId, (String)accessTokentool.getNewAccessToken().get("rs"),u.getName());
		return m;
		
	}
}
